package com.dongdongshop.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Utils.ShiroUtils;
import com.dongdongshop.domain.DataResult;
import com.dongdongshop.em.DataResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.CityService;
import com.dongdongshop.service.TbAddressService;
import com.dongdongshop.service.TbUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("UserController")
public class UserController {


   /* @Autowired
    private RocketMQTemplate rocketMQTemplate;*/
    @Reference
    private TbUserService tbUserService;

    /*所有地址的service*/
    @Reference
    private CityService cityService;


    @Reference
    private TbAddressService tbAddressService;

    /*注册*/
    @RequestMapping("registered")
    public String ToShow(){

        return "register";
    }

    @ResponseBody
    @RequestMapping("toadd")
    public DataResult toadd(TbUser tbUser){
        try {
            //String str = (String)redisTemplate.opsForValue().get(tbUser.getPhone());
            String salt = ShiroUtils.generateSalt(5);//生成
            tbUser.setSalt(salt);//修改
            String md5 = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), salt, 5);
            tbUser.setPassword(md5);//密码
            tbUser.setCreated(new Date());//创建时间
            Boolean insertinto = tbUserService.insertinto(tbUser);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(insertinto);
        }catch (Exception e){
            return  DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    /*登陆*/
    @RequestMapping("ToLogin")
    public String ToLogin(){

        return "login";
    }

    /*主页*/
    @RequestMapping("Homepage")
     public String Homepage(){

         return "index";
     }
    /*登陆*/
     @RequestMapping("Denglu")
     public String  Denglu(String sellerId, String password, Model model){
         Subject subject = SecurityUtils.getSubject();
         /*分装token令牌*/
         UsernamePasswordToken token = new UsernamePasswordToken(sellerId,password);
         try {
             subject.login(token);
         }catch (UnknownAccountException | IncorrectCredentialsException e){
             model.addAttribute("message","账号密码错误");
             e.printStackTrace();
             return "login";
         }
         return "index" ;
     }

     /*跳转个人信息页面*/
    @RequestMapping("Tohomesettingaddress")
    public String tohomesettingaddress(){
        TbUser principal = (TbUser)SecurityUtils.getSubject().getPrincipal();
        if(principal == null){
            return "login";
        }
        return "home-setting-address";
    }

    @RequestMapping("findAddress")
    @ResponseBody
    public DataResult findAddress(){
        TbUser principal = (TbUser)SecurityUtils.getSubject().getPrincipal();
        /*查询登陆用户的地址*/
        List<TbAddress> tbAddressList = tbAddressService.selectById(principal.getUsername());
        /*查询所有地址*/
        List<TbProvinces> provinces = cityService.selectProvinces();
        List<TbCities> cities = cityService.selectCities();
        List<TbAreas> areas = cityService.selectAreas();
        Map<String,Object> map = new HashMap<>();
        map.put("tbAddressList",tbAddressList);
        map.put("provinces",provinces);
        map.put("cities",cities);
        map.put("areass",areas);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(map);
    }

    @RequestMapping("selectCity")
     @ResponseBody
     public DataResult selectCity(Integer obj,String id){
         try {
             if(obj == 0){
                 List<TbProvinces> provinces = cityService.selectProvinces();
                 return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(provinces);
             }else if (obj == 1){
                 List<TbCities> cities = cityService.selectCitiesById(id);
                 return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(cities);
             }else if(obj == 2){
                 List<TbAreas> areas = cityService.selectAreasById(id);
                 return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(areas);
             }else {
                 return DataResult.buildDataResult(DataResultEnum.ERROR);
             }
         }catch (Exception e){
             e.printStackTrace();
             return DataResult.buildDataResult(DataResultEnum.ERROR);
         }

     }

    @RequestMapping("addAddress")
     @ResponseBody
     public DataResult addAddress(TbAddress address){
         try {
             TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
             address.setUserId(user.getUsername());
             address.setIsDefault("0");
             address.setCreateDate(new Date());
             tbAddressService.addAddress(address);
             return DataResult.buildDataResult(DataResultEnum.SUCCESS);
         }catch (Exception e){
             e.printStackTrace();
             return DataResult.buildDataResult(DataResultEnum.ERROR);
         }

     }

    @RequestMapping("deleteAddress")
     @ResponseBody
     public DataResult deleteAddress(Long id){
         try {
             tbAddressService.deleteAddress(id);
             return DataResult.buildDataResult(DataResultEnum.SUCCESS);
         }catch (Exception e){
             e.printStackTrace();
             return DataResult.buildDataResult(DataResultEnum.ERROR);
         }

     }

    @RequestMapping("updateAddress")
    @ResponseBody
    public DataResult updateAddress(Long id){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            tbAddressService.updateAddress(id,user.getUsername());
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }

    }
}
